
set(CPL_CURRENT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/port)
set(CPL_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/port)
include_directories(${CPL_CURRENT_SOURCE_DIR})
include_directories(${CPL_CURRENT_BINARY_DIR})

if(MSVC)
    configure_file(${CPL_CURRENT_SOURCE_DIR}/cpl_config.h.vc ${CPL_CURRENT_BINARY_DIR}/cpl_config.h @ONLY)
else(MSVC)
    set(GDAL_PREFIX "/usr/local")
    # Include all the necessary files for macros
    include (CheckFunctionExists)
    include (CheckIncludeFile)
    include (CheckIncludeFiles)
    include (CheckLibraryExists)
    include (CheckSymbolExists)
    include (CheckTypeSize)  
    include (TestBigEndian)
    include (CheckCXXSourceCompiles)
    #include (CompilerFlags)
    
    option(CPL_MULTIPROC_PTHREAD "Set to ON if you want to use pthreads based multiprocessing support." ON)
    mark_as_advanced(CPL_MULTIPROC_PTHREAD)
    check_symbol_exists(pthread_mutex_recursive pthread.h HAVE_PTHREAD_MUTEX_RECURSIVE)

    check_include_files( assert.h HAVE_ASSERT_H)
    if(${HAVE_ASSERT_H})
      add_definitions(-DHAVE_ASSERT_H)
    endif()
    check_function_exists("atoll" HAVE_ATOLL)
    if(${HAVE_ATOLL})
      add_definitions(-DHAVE_ATOLL)
    endif()
    check_include_files( csf.h HAVE_CSF_H)  
    if(${HAVE_CSF_H})
      add_definitions(-DHAVE_CSF_H)
    endif()
    check_include_files( dbmalloc.h HAVE_DBMALLOC_H) 
    if(${HAVE_DBMALLOC_H})
      add_definitions(-DHAVE_DBMALLOC_H)
    endif()
    check_function_exists("strtof" HAVE_DECL_STRTOF)    
    check_include_files( direct.h HAVE_DIRECT_H)    
    if(${HAVE_DIRECT_H})
      add_definitions(-DHAVE_DIRECT_H)
    endif()
    
    check_function_exists("fopen64" HAVE_FOPEN64)
    check_function_exists("stat64" HAVE_STAT64)
    
    if(${HAVE_FOPEN64} AND ${HAVE_STAT64})
	set(UNIX_STDIO_64 TRUE)
        set(VSI_LARGE_API_SUPPORTED TRUE)  
        set(VSI_FSEEK64 "fseeko64")
        set(VSI_FTELL64 "ftello64")
        set(VSI_FOPEN64 "fopen64")
        set(VSI_STAT64 "stat64")
        set(VSI_TRANCATE64 ftruncate64)
    else()
	set(UNIX_STDIO_64 FALSE)
        set(VSI_LARGE_API_SUPPORTED FALSE)  
        set(VSI_FSEEK64 "fseek")
        set(VSI_FTELL64 "ftell")
        set(VSI_FOPEN64 "fopen")
        set(VSI_STAT64 "stat")
        set(VSI_TRANCATE64 "ftrancate")
    endif()

    set(VSI_STAT64_T VSI_STAT64)   
 
    check_function_exists("vprintf" HAVE_VPRINTF)
    if(NOT ${HAVE_VPRINTF})
        check_function_exists("_doprnt" HAVE_DOPRNT)
    endif(NOT ${HAVE_VPRINTF})
    
    check_include_files( dlfcn.h HAVE_DLFCN_H) 
    if(${HAVE_DLFCN_H})
      add_definitions(-DHAVE_DLFCN_H)
    endif()
    check_include_files( errno.h HAVE_ERRNO_H)
    if(${HAVE_ERRNO_H})
      add_definitions(-DHAVE_ERRNO_H)
    endif()
    check_include_files( fcntl.h HAVE_FCNTL_H) 
    if(${HAVE_FCNTL_H})
      add_definitions(-DHAVE_FCNTL_H)
    endif()
    check_include_files( float.h HAVE_FLOAT_H) 
    if(${HAVE_FLOAT_H})
      add_definitions(-DHAVE_FLOAT_H)
    endif()

    check_function_exists("getcwd" HAVE_GETCWD)

    #check_function_exists("iconv" HAVE_ICONV)    
    find_package(Iconv)
    set(HAVE_ICONV ${ICONV_FOUND})
    if(ICONV_SECOND_ARGUMENT_IS_CONST)
      set(ICONV_CONST "const")
    endif(ICONV_SECOND_ARGUMENT_IS_CONST)   
    
    check_library_exists(c "" "" HAVE_LIBC)
   
    check_type_size("int16" HAVE_INT16)
    check_type_size("int32" HAVE_INT32)    
    check_type_size("int8" HAVE_INT8)

    check_include_files(ieeefp.h HAVE_IEEEFP_H)
    set(HAVE_IEEEFP TRUE)
    check_include_files(inttypes.h HAVE_INTTYPES_H)
    if(${HAVE_INTTYPES_H})
      add_definitions(-DHAVE_INTTYPES_H)
    endif()
    check_include_files(jpeglib.h HAVE_JPEGLIB_H)

    check_type_size ("int"           SIZEOF_INT         )
    check_type_size ("uint32_t"      HAVE_UINT32_T      )
    check_type_size ("int64_t"       HAVE_INT64_T     )
    check_type_size ("float"         SIZEOF_FLOAT       )
    check_type_size ("double"        SIZEOF_DOUBLE      )
    check_type_size ("long"          SIZEOF_LONG        )
    check_type_size ("unsigned long" SIZEOF_UNSIGNED_LONG)
    check_type_size ("long long"     SIZEOF_LONG_LONG   )
    check_type_size ("short"         SIZEOF_SHORT       )
    check_type_size ("off_t"         SIZEOF_OFF_T       )
    check_type_size ("pid_t"         SIZEOF_PID_T       )
    check_type_size ("size_t"        SIZEOF_SIZE_T      )
    check_type_size ("socklen_t"     SIZEOF_SOCKLEN_T    )
    check_type_size ("sig_atomic_t"  SIZEOF_SIG_ATOMIC_T )
    check_type_size ("void *"        SIZEOF_VOID_P       )
    check_type_size ("uintptr_t"     SIZEOF_UINTPTR_T    )
    check_type_size ("_Bool"         HAVE__BOOL          )
    check_type_size ("intptr_t"      SIZEOF_INTPTR_T     )

    test_big_endian(WORDS_BIGENDIAN)    
    if (WORDS_BIGENDIAN)
        set (HOST_FILLORDER FILLORDER_MSB2LSB)
    else (WORDS_BIGENDIAN)
        set (HOST_FILLORDER FILLORDER_LSB2MSB)
    endif (WORDS_BIGENDIAN)
 
    #check_include_files (malloc.h HAVE_MALLOC_H)
    #check_include_files ("sys/param.h;sys/mount.h" HAVE_SYS_MOUNT_H)
    #check_function_exists("strtoll" HAVE_STRTOLL)
    #check_function_exists("strftime" HAVE_STRFTIME)
    #check_function_exists("setlocale" HAVE_SETLOCALE)

    check_library_exists(pq "" "" HAVE_LIBPQ)
    CHECK_LIBRARY_EXISTS(m matherr "" HAVE_LIBM)
    check_library_exists(dl dlopen "" HAVE_LIBDL)
    check_library_exists(rt sem_init "" HAVE_LIBRT)

    check_include_files( limits.h HAVE_LIMITS_H)
    if(${HAVE_LIMITS_H})
      add_definitions(-DHAVE_LIMITS_H)
    endif()
    check_include_files( locale.h HAVE_LOCALE_H)
    if(${HAVE_LOCALE_H})
      add_definitions(-DHAVE_LOCALE_H)
    endif()

    check_type_size("long long" HAVE_LONG_LONG)

    check_include_files( memory.h HAVE_MEMORY_H)
    if(${HAVE_MEMORY_H})
      add_definitions(-DHAVE_MEMORY_H)
    endif()
    check_include_files( png.h HAVE_PNG_H)

    check_function_exists("snprintf" HAVE_SNPRINTF)

    check_include_files( stdint.h HAVE_STDINT_H)
    if(${HAVE_STDINT_H})
      add_definitions(-DHAVE_STDINT_H)
    endif()
    check_include_files( stddef.h HAVE_STDDEF_H)
    if(${HAVE_STDDEF_H})
      add_definitions(-DHAVE_STDDEF_H)
    endif()
    check_include_files( stdlib.h HAVE_STDLIB_H)
    if(${HAVE_STDLIB_H})
      add_definitions(-DHAVE_STDLIB_H)
    endif()
    check_include_files( strings.h HAVE_STRINGS_H)
    if(${HAVE_STRINGS_H})
      add_definitions(-DHAVE_STRINGS_H)
    endif()
    check_include_files( string.h HAVE_STRING_H)
    if(${HAVE_STRING_H})
      add_definitions(-DHAVE_STRING_H)
    endif()

    check_function_exists("strtof" HAVE_STRTOF)

    check_include_file(sys/stat.h HAVE_SYS_STAT_H)
    if(${HAVE_SYS_STAT_H})
      add_definitions(-DHAVE_SYS_STAT_H)
    endif()
    check_include_file(sys/types.h HAVE_SYS_TYPES_H)
    if(${HAVE_SYS_TYPES_H})
      add_definitions(-DHAVE_SYS_TYPES_H)
    endif()
    check_include_file(unistd.h HAVE_UNISTD_H)
    if(${HAVE_UNISTD_H})
      add_definitions(-DHAVE_UNISTD_H)
    endif()
    check_include_file(values.h HAVE_VALUES_H)
    if(${HAVE_VALUES_H})
      add_definitions(-DHAVE_VALUES_H)
    endif()

    check_function_exists("vprintf" HAVE_VPRINTF)
    check_function_exists("vsnprintf" HAVE_VSNPRINTF)
    check_function_exists("readlink" HAVE_READLINK)
 
    if(MACOSX_BUNDLE)
        set(CPL_CONFIG_EXTRAS "#include \"cpl_config_extras.h\"" INTERNAL)
    endif(MACOSX_BUNDLE)

    if (HAVE_STDDEF_H AND HAVE_STDINT_H)
      set(STDC_HEADERS TRUE)
    endif (HAVE_STDDEF_H AND HAVE_STDINT_H)

    if (WIN32)
      set(LT_OBJDIR "_libs/")
    else (WIN32)
      set(LT_OBJDIR ".libs/")
    endif (WIN32)
    
    configure_file(${CPL_CURRENT_SOURCE_DIR}/cpl_config.h.cmake ${CPL_CURRENT_BINARY_DIR}/cpl_config.h @ONLY)

message(STATUS "Target '${PROJECT_NAME}' is configured")
message("---------------------------------------------")
endif(MSVC)

set(CPL_HHEADERS
    ${CPL_CURRENT_SOURCE_DIR}/cplkeywordparser.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_atomic_ops.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_config_extras.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_conv.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_csv.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_error.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_hash_set.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_http.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_list.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_minixml.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_minizip_ioapi.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_minizip_unzip.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_minizip_zip.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_multiproc.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_odbc.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_port.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_quad_tree.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_string.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_time.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_vsi.h
    ${CPL_CURRENT_SOURCE_DIR}/cpl_vsi_virtual.h
    #cpl_win32ce_api.h
    #cpl_wince.h
    ${CPL_CURRENT_SOURCE_DIR}/gdal_csv.h
    ${CPL_CURRENT_BINARY_DIR}/cpl_config.h
)

set(CPL_CSOURCES
    ${CPL_CURRENT_SOURCE_DIR}/cplgetsymbol.cpp
    ${CPL_CURRENT_SOURCE_DIR}/cplkeywordparser.cpp
    ${CPL_CURRENT_SOURCE_DIR}/cplstring.cpp
    ${CPL_CURRENT_SOURCE_DIR}/cplstringlist.cpp
    ${CPL_CURRENT_SOURCE_DIR}/cpl_atomic_ops.cpp
    ${CPL_CURRENT_SOURCE_DIR}/cpl_base64.cpp
    ${CPL_CURRENT_SOURCE_DIR}/cpl_conv.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_csv.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_error.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_findfile.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_getexecpath.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_hash_set.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_http.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_list.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_minixml.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_minizip_ioapi.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_minizip_unzip.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_minizip_zip.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_multiproc.cpp 
    #cpl_odbc.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_path.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_quad_tree.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_recode.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_recode_iconv.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_recode_stub.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_string.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_strtod.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_time.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_vsil.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_vsil_abstract_archive.cpp
    ${CPL_CURRENT_SOURCE_DIR}/cpl_vsil_buffered_reader.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_vsil_cache.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_vsil_curl.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_vsil_gzip.cpp 
    #cpl_vsil_simple.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_vsil_sparsefile.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_vsil_stdin.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_vsil_stdout.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_vsil_subfile.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_vsil_tar.cpp     
    ${CPL_CURRENT_SOURCE_DIR}/cpl_vsisimple.cpp 
    ${CPL_CURRENT_SOURCE_DIR}/cpl_vsi_mem.cpp 
    #cpl_win32ce_api.cpp 
    #xmlreformat.cpp 
)

if(WIN32)
    set(CPL_CSOURCES ${CPL_CSOURCES} ${CPL_CURRENT_SOURCE_DIR}/cpl_vsil_win32.cpp)
else(WIN32)
    set(CPL_CSOURCES ${CPL_CSOURCES} ${CPL_CURRENT_SOURCE_DIR}/cpl_vsil_unix_stdio_64.cpp)
endif(WIN32)

source_group("Header Files\\port" FILES ${CPL_HHEADERS})
set(GDAL_HHEADERS ${GDAL_HHEADERS} ${CPL_HHEADERS})
source_group("Source Files\\port" FILES ${CPL_CSOURCES})
set(GDAL_CSOURCES ${GDAL_CSOURCES} ${CPL_CSOURCES})
